package com.nucarf.bi.digtalize.dao;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;

public class DaoManager {
    private static SqlSession sqlSession;
    private static MysqlMapper mapper;

    public static void initialConnection() throws Exception {
        String resource = "mybatis-config.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
        sqlSession = ssf.openSession();
        mapper = sqlSession.getMapper(MysqlMapper.class);
    }

    public static void destroyConnection() {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }

    public static Integer getFisrtMalId(Long customer_number, Long recharge_companyId) {
        return mapper.selectMinMalId(customer_number, recharge_companyId);
    }
}
